package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetBookIslandGameBean;
import com.rjwh.dingdong.client.bean.localbean.BookIslandGameBean;
import com.rjwh.dingdong.client.bean.localbean.GetBookIslandGameBean;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MathGameIslandsListModel;
import com.rjwh.dingdong.client.mvp.presenter.IMathGameIslandsListPresenter;
import com.rjwh.dingdong.client.mvp.view.MathGameIslandsListView;
import com.rjwh.dingdong.client.service.PlaybackService;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName MathGameIslandsListPresenter
 * @Description
 * @date 2016-8-31
 * @history 2016-8-31 author: description:
 */
public class MathGameIslandsListPresenter extends BasePresenter<MathGameIslandsListView> implements IMathGameIslandsListPresenter {

    private MathGameIslandsListModel model;
    private PlaybackService mPlaybackService;
    private boolean mIsServiceBound;
    private SwipyRefreshLayoutDirection mDirection;

    public MathGameIslandsListPresenter(Context context, MathGameIslandsListView mMvpView) {
        super(context, mMvpView);
        model = new MathGameIslandsListModel(this);
    }

    /**
     * 获取教材所有游戏
     *
     * @param bookid         游戏id
     * @param packageversion
     * @param pageindex      页码
     * @param isRefresh
     * @param direction
     */
    @Override
    public void doGetBookIslandGame(int bookid, String packageversion, int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetBookIslandGame(bookid, packageversion, pageindex, isRefresh);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            switch (businessType) {
                case NetConstant.GETBOOKISLANDGAMEBYVERSION:
                    getMvpView().setRefreshing(false);
                    ResGetBookIslandGameBean resGetBookIslandGameBean = (ResGetBookIslandGameBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetBookIslandGameBean != null) {
                        GetBookIslandGameBean getBookIslandGameBean = resGetBookIslandGameBean.getData();
                        if (getBookIslandGameBean != null) {
                            List<BookIslandGameBean> gamelist = getBookIslandGameBean.getGamelist();
                            if (gamelist != null) {
                                if (gamelist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, gamelist);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;

                default:
                    break;
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
            getMvpView().onPlaybackServiceBound(mPlaybackService);
        }

        public void onServiceDisconnected(ComponentName className) {
            mPlaybackService = null;
            getMvpView().onPlaybackServiceUnbound();
        }
    };

    private void bindPlaybackService() {
        getContext().bindService(new Intent(getContext(), PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsServiceBound = true;
    }

    private void unbindPlaybackService() {
        if (mIsServiceBound) {
            // Detach our existing connection.
            getContext().unbindService(mConnection);
            mIsServiceBound = false;
        }
    }

    public void subscribe() {
        bindPlaybackService();
    }

    @Override
    public void detachView() {
        unbindPlaybackService();
        model.detachView();
        detachViews();
    }

}
